package com.sptpc.qimoxiangmu.controller;

import com.sptpc.qimoxiangmu.controller.request.TypeRequest;
import com.sptpc.qimoxiangmu.model.Type;
import com.sptpc.qimoxiangmu.model.vo.TypeVo;
import com.sptpc.qimoxiangmu.service.TypeService;
import com.sptpc.qimoxiangmu.util.ServerResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;


    @GetMapping("/insert.html")
    public ModelAndView insertView(){
        ModelAndView mv=new ModelAndView("type/insert");
        mv.addObject("typeList",typeService.findAll());
        return mv;
    }
    @GetMapping("/list.html")
    public ModelAndView listView(){
        ModelAndView mv =new ModelAndView("type/list");
        List<Type> typeList=typeService.findAll();
        mv.addObject("typeList",typeList);
        return mv;
    }
    @GetMapping("/edit.html")
    public ModelAndView redirct_update(@RequestParam("id")Long id){
        ModelAndView mv=new ModelAndView("type/edit");
        Type type=typeService.findOne(id);//通过id查询名字
        mv.addObject("id",id);
        mv.addObject("type",type);
        return mv;
    }
    @PostMapping(value ="/save", params = "name", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)//表单形式，x.www.urlencoded，成功
    @ResponseBody
    public ServerResponse getCategory(@RequestParam String name) {
        return ServerResponse.createBySuccess( typeService.saveCategory(name)) ;
    }
    @PostMapping(value ="/save",consumes =MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ResponseStatus(code=HttpStatus.CREATED)
    public ServerResponse insert(@RequestBody TypeRequest typerequest) {
        Type type=new Type();
        BeanUtils.copyProperties(typerequest,type);
        return ServerResponse.createBySuccess( typeService.saveCategory(type)) ;
    }


    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)//成功
    @ResponseBody
    public ServerResponse update(TypeVo typeVo) {
        return ServerResponse.createBySuccess( typeService.update(typeVo));
    }

    @GetMapping("/")//获取全部数据，成功
    @ResponseBody
    public ServerResponse list(){
        return ServerResponse.createBySuccess( typeService.findAll());
    }
    //显示的页面，成功


    @GetMapping(value = "/",params = "name")//成功
    @ResponseBody
    public ServerResponse findOne(@RequestParam String name){
        return  ServerResponse.createBySuccess( typeService.findOne(name));
    }
    //查询方法localhost:8080/category/13
    @GetMapping(value = "/{id}")//成功
    @ResponseBody
    public ServerResponse findOne(@PathVariable Long id){
        return ServerResponse.createBySuccess( typeService.findOne(id));
    }
    @GetMapping(value = "/delete",params = "name")
    @ResponseBody
    public ServerResponse delete(@RequestParam String name){
        return ServerResponse.createBySuccess(  typeService.delete(name));
    }


    @GetMapping("/delete")//成功
    @ResponseBody
    public ServerResponse delete(@RequestParam(name="id") Long id){
        Type category= typeService.findOne(id);
        if(category!=null){
            typeService.deleteCategory(id);
            return ServerResponse.createBySuccessMessage("删除成功！");
        }else {
            return ServerResponse.createByErrorMessage("找不到删除分类！");
        }
    }
    @GetMapping("/request")//成功
    @ResponseBody
    public Map getClientInfo(HttpServletRequest request) {
        Map infos = new HashMap();
        infos.put("客户端IP", request.getRemoteAddr());
        infos.put("客户端信息", request.getHeader("user-agent"));
        infos.put("客户访问的资源路径", request.getRequestURL());
        return infos;
    }
    //重定向
    @GetMapping("/redirect")//成功
    public String redirect() {
        return "redirect:request";//但是要注意，Controller不能是@RestController，否则String就被转化了，变成文本。
    }
    @Data
    @MappedSuperclass
    @AllArgsConstructor
    @NoArgsConstructor

    public static class BaseEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Column(updatable = false)
        @CreationTimestamp
        private LocalDateTime createTime;
        @UpdateTimestamp
        private LocalDateTime updateTime;
    }
}




